#!/bin/bash

# ln -s "$(pwd)/update.sh" ~/update.sh

app_name=$1
jar_name=$2
jar_dir=~/jars

if [[ $app_name = "" ]];
then
  echo "未指定服务名称，退出"
  exit 0
fi

if [[ $jar_name = "" ]];
then
  echo "未指定jar名称，退出"
  exit 0
fi

self_dir=$(dirname $(readlink -f "$0"))  # 当前脚本的绝对路径
work_dir=$(dirname $(dirname "$self_dir"))  # 工作目录，相当于：../../
service_dir="$work_dir/service/running/$app_name"


# 判断路径是否存在
if [[ -d "$service_dir" ]];
then
  echo "已存在路径：$service_dir，不用重复初始化"
else
  echo "不存在路径：$service_dir，自动初始化服务"
  "$work_dir/sh/init/new-host-service.sh" "$app_name"
fi


# 拷贝jar包到指定目录
\cp -f "$jar_dir/$jar_name" "$service_dir/app.jar"

# 执行自动更新脚本
"$self_dir/update-base.sh" "$app_name"

